package com.chebaohui.iqs.service.impl.deal.libao.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.service.impl.deal.libao.sdk.SignUtil;
import com.chebaohui.iqs.service.impl.deal.util.TimeBase;

/**
 * 利宝接口公共参数获取
 * 
 * @author xielonghai
 *
 */
public class PublicParam {

	/**
	 * 公共请求头信息
	 * 
	 * @return
	 */
	public static Map<String, String> getParam() throws Exception {
		Map<String, String> map = new HashMap<String, String>();

		// 合作方编码
		map.put("partnerAccountCode", LibaoConfig.partnerAccountCode);

		// 业务流程ID
		map.put("flowId", LibaoConfig.partnerAccountCode + TimeBase.getDay("yyyyMMddHHmmss", 0) + getNumber());

		// 操作时间
		map.put("operatorDate", TimeBase.getDay("yyyy-MM-dd hh:mm:ss", 0));

		// 备案号
		map.put("recordCode", LibaoConfig.recordCode);

		// 业务关系代码
		map.put("agreementNo", LibaoConfig.agreementNo);

		// tocken认证码
		map.put("access_token", Certification.getAccess_token());

		return map;
	}

	/**
	 * 公共请求头信息
	 * 
	 * @return
	 */
	public static Map<String, String> getParam(String flowId) throws Exception {
		Map<String, String> map = new HashMap<String, String>();

		// 合作方编码
		map.put("partnerAccountCode", LibaoConfig.partnerAccountCode);

		// 业务流程ID
		map.put("flowId", flowId);

		// 操作时间
		map.put("operatorDate", TimeBase.getDay("yyyy-MM-dd hh:mm:ss", 0));

		// 备案号
		map.put("recordCode", LibaoConfig.recordCode);

		// 业务关系代码
		map.put("agreementNo", LibaoConfig.agreementNo);

		// tocken认证码
		map.put("access_token", Certification.getAccess_token());

		return map;
	}

	private static String time = "";
	static {
		try {
			time = TimeBase.getDay("yyyy-MM-dd", 0);
		} catch (Exception e) {
		}
	}
	private static int number = 10;

	/**
	 * 每天循环产生
	 * 
	 * @return
	 */
	private static int getNumber() throws Exception {
		if (time.equals(TimeBase.getDay("yyyy-MM-dd", 0))) {
			number++;
			return number;
		} else {
			time = TimeBase.getDay("yyyy-MM-dd", 0);
			number = 11;
			return number;
		}
	}

	/**
	 * map转 json
	 * 
	 * @param map
	 * @return
	 */
	public static JSONObject getParam(Map<String, String> map) {
		JSONObject json = new JSONObject();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			json.put(entry.getKey(), entry.getValue());
		}
		return json;
	}

	/**
	 * 加密
	 * 
	 * @param map
	 * @return
	 */
	public static String getPdk(Map<String, String> map) {
		return SignUtil.buildSign(map);
	}

}
